//
//  UserViewController.m
//  OurTourism
//
//  Created by 孟镇 on 15/12/3.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "UserViewController.h"
#import "landingViewController.h"
#import "Header.h"
#import <BmobSDK/Bmob.h>
#import "MZCollectedViewController.h"
@interface UserViewController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *login;
@property (strong, nonatomic) IBOutlet UITableView *TableView;

@end

@implementation UserViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"judge"] == NO) {
        [self.login setTitle:@"登录" forState:0];
        NSLog(@"没有的登陆");
    } else {
        NSLog(@"已经登陆");
          [self.login setTitle:@"注销" forState:0];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    self.navigationItem.title = @"我的";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:62 / 255. green:176 / 255. blue:192 / 255. alpha:1];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 25, 25);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-iconfonticonfontjiantouzuo11@2x"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
  
//    BmobObject *gameScore = [BmobObject objectWithClassName:@"Mzzz"];
//                [gameScore setObject:@"zzz" forKey:@"account"];
//                [gameScore setObject:@"123" forKey:@"password"];
//                [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
//                    //进行操作
//                }]; 

    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = @[@"我的收藏", @"清除缓存", @"关于我们"];
    NSArray *imgArr = @[@"myCollect", @"clean", @"about"];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.textLabel.text = arr[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:imgArr[indexPath.row]];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        
        MZCollectedViewController *cvVC = [[MZCollectedViewController alloc] init];
        [self.navigationController pushViewController:cvVC animated:YES];
    }
    if (indexPath.row == 1) {
        
        //获取cachesPath
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        
        NSString *str = [NSString stringWithFormat:@"清楚缓存(%.2f)M", [self floatWithPath:cachesPath]];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            NSString *path_cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
            
            NSFileManager *fileManager = [NSFileManager defaultManager];
            
            if ([fileManager fileExistsAtPath:path_cache]) {
                NSEnumerator *childFile = [[fileManager subpathsAtPath:path_cache] objectEnumerator];
                NSString *fileName;
                while ((fileName = [childFile nextObject]) != nil) {
                    NSString *fileSub = [path_cache stringByAppendingPathComponent:fileName];
                    NSError *error = nil;
                    [fileManager removeItemAtPath:fileSub error:&error];
                }
            }
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:okAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:YES completion:^{
        }];
    }
    if (indexPath.row == 2) {
        
        NSLog(@"关于");
    }
}

#pragma mark - 计算清除缓存的大小
- (CGFloat)floatWithPath:(NSString *)path
{
    CGFloat num = 0;
    NSFileManager *man = [NSFileManager defaultManager];
    if ([man fileExistsAtPath:path]) {
        NSEnumerator *childFile = [[man subpathsAtPath:path] objectEnumerator];
        NSString *fileName;
        while ((fileName = [childFile nextObject]) != nil) {
            NSString *fileSub = [path stringByAppendingPathComponent:fileName];
            num += [self fileSizeAtPath:fileSub];
        }
    }
    return num / (1024.0 * 1024.0);
}
//计算单个文件大小
- (long long)fileSizeAtPath:(NSString *)file
{
    
    NSFileManager *man = [NSFileManager defaultManager];
    if ([man fileExistsAtPath:file]) {
        return [[man attributesOfItemAtPath:file error:nil] fileSize];
    }
    return 0;
}


- (IBAction)registered:(UIButton *)sender {
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"judge"] == NO) {
        NSLog(@"没有的登陆");
        landingViewController *landing = [[landingViewController alloc] init];
        [self.navigationController pushViewController:landing animated:YES];
        
    } else {
        NSLog(@"已经登陆");
         [self.login setTitle:@"登录" forState:0];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"judge"];
    }
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
